﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using NPOI.XWPF.UserModel;

namespace AChance.Func
{
    public static class FunWord
    {
        public class Document
        {
            private XWPFDocument _xwpfDocument = null;

            public Document()
            {
                _xwpfDocument = new XWPFDocument();
            }
            public Document(string path)
            {
                _xwpfDocument = new XWPFDocument(new FileStream(path, FileMode.Open, FileAccess.ReadWrite));
            }
            public Document(Stream stream)
            {
                _xwpfDocument = new XWPFDocument(stream);
            }
            public void Save(string path)
            {
                Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                _xwpfDocument.Write(stream);
                stream.Close();
            }
            public void Save(Stream stream)
            {
                _xwpfDocument.Write(stream);
            }
            /// <summary>
            ///  创建Paragraph
            /// </summary>
            public Paragraph CreateParagraph()
            {
                Paragraph paragraph = new Paragraph();
                paragraph.XWPFParagraph = _xwpfDocument.CreateParagraph();
                return paragraph;
            }
            /// <summary>
            ///  访问Paragraph
            /// </summary>
            public Paragraph GetParagraph(int paragraphIndex)
            {
                Paragraph paragraph = new Paragraph();
                paragraph.XWPFParagraph = _xwpfDocument.GetParagraphArray(paragraphIndex);
                return paragraph;
            }
            /// <summary>
            ///  删除Element
            /// </summary>
            public void DeleteElement(int elementIndex)
            {
                _xwpfDocument.RemoveBodyElement(elementIndex);
            }
            /// <summary>
            ///  替换文本
            /// </summary>
            public void ReplaceText(string oldText, string newText)
            {
                foreach (XWPFParagraph xwpfParagraph in _xwpfDocument.Paragraphs)
                {
                    foreach (XWPFRun xwpfRun in xwpfParagraph.Runs)
                    {
                        xwpfRun.ReplaceText(oldText, newText);
                    }
                }
            }
        }

        public class Paragraph
        {
            private XWPFParagraph _xwpfParagraph = null;
            internal XWPFParagraph XWPFParagraph
            {
                get { return _xwpfParagraph; }
                set { _xwpfParagraph = value; }
            }
            public Run CreateRun()
            {
                Run run = new Run();
                run.XWPFRun = _xwpfParagraph.CreateRun();
                return run;
            }
            public int Align
            {
                get { return (int)_xwpfParagraph.Alignment; }
                set { _xwpfParagraph.Alignment = (ParagraphAlignment)value; }
            }
            public int Valign
            {
                get { return (int)_xwpfParagraph.VerticalAlignment; }
                set { _xwpfParagraph.VerticalAlignment = (TextAlignment)value; }
            }
            public void ReplaceText(string oldText, string newText)
            {
                foreach (XWPFRun xwpfRun in _xwpfParagraph.Runs)
                {
                    xwpfRun.ReplaceText(oldText, newText);
                }
            }
        }

        public class Run
        {
            private XWPFRun _xwpfRun = null;
            internal XWPFRun XWPFRun
            {
                get { return _xwpfRun; }
                set { _xwpfRun = value; }
            }
            public string Text
            {
                get { return _xwpfRun.ToString(); }
                set { _xwpfRun.SetText(value); }
            }
            public void ReplaceText(string oldText, string newText)
            {
                _xwpfRun.ReplaceText(oldText, newText);
            }
        }

        public static class Constant
        {
            /// <summary>
            /// 水平对齐方式选择
            /// </summary>
            public static class Align
            {
                public const int LEFT = (int)ParagraphAlignment.LEFT;
                public const int CENTER = (int)ParagraphAlignment.CENTER;
                public const int RIGHT = (int)ParagraphAlignment.RIGHT;
            }
            /// <summary>
            /// 垂直对齐方式选择
            /// </summary>
            public static class Valign
            {
                public const int TOP = (int)TextAlignment.TOP;
                public const int CENTER = (int)TextAlignment.CENTER;
                public const int BOTTOM = (int)TextAlignment.BOTTOM;
            }
        }
    }
}
